package org.makcro.sms.services.chat.web.client.widgets;

import java.util.ArrayList;
import java.util.List;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.shared.dto.HistoryItemDto;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;

public class AnswerDialog extends Dialog {

	/**
	 * History grid and store
	 */
	private Grid<HistoryItemDto>				historyGrid;
	private ListStore<HistoryItemDto>			store;
	/**
	 * Template for history items
	 */
	
	private TextArea							questionTextArea;
	private AnswerTextPanel						answerTextPanel;
	/**
	 * Buttons
	 */
	private Button								btnSendAnswer;
	private Button								btnCancel;

	public AnswerDialog() {
		initComponents();
	}

	public void initComponents() {

		setButtons( "" );
		setModal( true );
		setHeading( "Odgovor" );
		setWidth( 800 );
		setHeight( 600 );
		setClosable( false );

		setLayout( new RowLayout( Orientation.VERTICAL ) );

		add( getHistoryGrid(), new RowData( 1.0, 0.5, new Margins() ) );
		add( getQuestionTextArea(), new RowData( 1.0, 0.2, new Margins( 5, 0, 5, 0 ) ) );
		add( getAnswerTextPanel(), new RowData( 1, 0.3, new Margins() ) );
		addButton( getBtnSendAnswer() );
		addButton( getBtnCancel() );
	}

	public Grid<HistoryItemDto> getHistoryGrid() {
		if (historyGrid == null) {
			historyGrid = new Grid<HistoryItemDto>( getStore(), getColumModel() );
			historyGrid.setAutoExpandColumn( "question" );
			historyGrid.setAutoWidth( true );
		}
		return historyGrid;
	}

	public TextArea getQuestionTextArea() {
		if (questionTextArea == null) {
			questionTextArea = new TextArea();
			questionTextArea.disable();
			questionTextArea.setFieldLabel( "Tekst pitanja" );
		}
		return questionTextArea;
	}
	

	private ColumnModel getColumModel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig column = new ColumnConfig( "question", "Istorija", 200 );
		column.setRenderer( new HistoryCellRenderer() );
		columns.add( column );

		return new ColumnModel( columns );
	}

	public ListStore<HistoryItemDto> getStore() {
		if (store == null) {
			store = new ListStore<HistoryItemDto>();
		}
		return store;
	}

	public AnswerTextPanel getAnswerTextPanel() {
		if (answerTextPanel == null) {
			answerTextPanel = new AnswerTextPanel();
		}
		return answerTextPanel;
	}

	public Button getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new Button( "Otkazi" );
			btnCancel.addListener( Events.Select, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent arg0) {
					onBtnCancelSelected();
				}
			} );
		}
		return btnCancel;
	}

	protected void onBtnCancelSelected() {

		Dispatcher.get().dispatch( AppEvents.QUESTION_RESERVATION_CANCELED );
		hide();
	}

	public Button getBtnSendAnswer() {
		if (btnSendAnswer == null) {
			btnSendAnswer = new Button( "Posalji" );
			btnSendAnswer.addListener( Events.Select, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent arg0) {
					onBtnSendAnswerSelected();
				}
			} );

		}
		return btnSendAnswer;
	}

	protected void onBtnSendAnswerSelected() {
		// get answer text and fire the event
		String answerText = getAnswerTextPanel().getTxtAnswerText().getValue();
		AppEvent event = new AppEvent( AppEvents.ANSWER_ABOUT_TO_BE_SENT, answerText );
		Dispatcher.get().dispatch( event );
	}

	public void reset() {
		getAnswerTextPanel().reset();
		getStore().removeAll();

	}

}
